const redis = require("redis");
const redis_cfg = configure.get('redis');
const client = redis.createClient(redis_cfg);

const { promisify } = require("util");
const getAsync = promisify(client.get).bind(client);
const setAsync = promisify(client.set).bind(client);
const expireAsync = promisify(client.expire).bind(client);

let _redis_util = module.exports

client.on("error", function(error) {
    log4js.error("redis connect err: ",error);
});

client.on("connect", function() {
    log4js.debug(`redis connect: ${redis_cfg.host}:${redis_cfg.port}`);
});

client.on("reconnecting", function(error) {
    log4js.debug(`redis reconnecting: ${redis_cfg.host}:${redis_cfg.port}`);
});

_redis_util.get = async (k) => {
    return JSON.parse(await getAsync(k));
}

_redis_util.set = async (k,v, t= 60) => {
    await setAsync(k,JSON.stringify(v));
    await expireAsync(k, t)
}

